﻿using RightControl.Common;
using RightControl.IRepository;
using RightControl.IService;
using RightControl.Model;

namespace RightControl.Service {
    public class ProductService : BaseService<ProductDetail>, IProductService {
        public IProductRepository repository { get; set; }

        public dynamic GetListByFilter(ProductDetail filter, PageInfo pageInfo) {
            string _where = " where 1=1";
            _where += string.Format(" and UserId = {0}", filter.UserId);
            if (filter.Id > 0) {
                _where += string.Format(" and Id = {0}", filter.Id);
            }
            if (filter.ProductType > 0) {
                _where += string.Format(" and ProductType = {0}", filter.ProductType);
            }
            if (filter.CategoryCode.IsNotEmpty()) {
                _where += string.Format(" and CategoryCode = '{0}'", filter.CategoryCode);
            }

            return GetListByFilter(filter, pageInfo, _where);
        }

        public bool IsNotExist(ProductDetail filter) {
            string _where = " where 1=1";
            if (filter.OriProductUrl.IsNotEmpty()) {
                _where += string.Format(" and OriProductUrl = '{0}'", filter.OriProductUrl);
            }

            if (filter.UserId > 0) {
                _where += string.Format(" and UserId = {0}", filter.UserId);
            }
            return GetTotal(new SearchFilter() { pageIndex = 1, pageSize = 100, where = _where }) == 0;
        }
    }
}